package vn.edu.uit.chemistry.utilities;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import vn.edu.uit.chemistry.protocol.BaiCu;
import vn.edu.uit.chemistry.protocol.BaiGiang;
import vn.edu.uit.chemistry.protocol.BatKy;
import vn.edu.uit.chemistry.protocol.CapNhatKiemTra15;
import vn.edu.uit.chemistry.protocol.KetQuaKiemTra15;
import vn.edu.uit.chemistry.protocol.Message;
import vn.edu.uit.chemistry.protocol.TaiLieuThamKhao;
import vn.edu.uit.chemistry.protocol.ThongTinCaNhan;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class TCPClient implements Runnable {

	Context ctx;
	Socket sk;
	String host = "";
	int port = 0;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;

	public TCPClient(Context _ctx, String _host, int _port)
			throws UnknownHostException, IOException {
		this.ctx = _ctx;
		this.host = _host;
		this.port = _port;
		sk = new Socket(host, port);
		oos = new ObjectOutputStream(sk.getOutputStream());
		ois = new ObjectInputStream(sk.getInputStream());
	}

	public void stop() throws IOException {
		oos.close();
		ois.close();
		sk.close();
	}

	public void guiThongTin() throws IOException, ClassNotFoundException {
		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		String portHS = prefs.getString("port", "").toString();

		oos.writeObject(new ThongTinCaNhan(tenHS, mshs, ipHS, portHS));

		Message msg = (Message) ois.readObject();

		if (msg instanceof ThongTinCaNhan) {
			//Toast.makeText(ctx, "Da nhan Phan hoi", Toast.LENGTH_LONG).show();
		}
	}

	public void nopBaiCu(String path) throws IOException, ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];
		
		SharedPreferences prefs = ctx
				.getSharedPreferences("TaiKhoanGV", 0);
		String ipGV = prefs.getString("ip", "").toString();

		prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		
		oos.writeObject(new BaiCu(myfile.getName(), ""+myfile.length(), tenHS, mshs, ipHS));
		
		SharedPreferences ref = ctx.getSharedPreferences("GuiFileGV", 0);
		SharedPreferences.Editor editor = ref.edit();
		editor.putInt("finish", 0);
		editor.commit();
		
		Message fileBaiCu = (Message) ois.readObject();
		if (fileBaiCu instanceof BaiCu) {
			System.out.println("OK...");
			
			Socket socket = new Socket(ipGV, 9999);
			System.out.println("Connecting...");
			
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();
			
		}
		
		ref = ctx.getSharedPreferences("GuiFileGV", 0);
		editor = ref.edit();
		editor.putInt("finish", 1);
		editor.commit();
	}
	
	public void guiBaiCuTK(String path) throws IOException, ClassNotFoundException {
		
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];
		
		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		
		oos.writeObject(new BaiCu(myfile.getName(), "" + myfile.length(),
				tenHS, mshs, ipHS));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof BaiCu) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void guiTaiLieuChoGV(String path) throws IOException, ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];
		
		SharedPreferences prefs = ctx
				.getSharedPreferences("TaiKhoanGV", 0);
		String ipGV = prefs.getString("ip", "").toString();

		prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		
		oos.writeObject(new BaiGiang(myfile.getName(), ""+myfile.length(), tenHS, mshs, ipHS));
		
		SharedPreferences ref = ctx.getSharedPreferences("GuiFileGV", 0);
		SharedPreferences.Editor editor = ref.edit();
		editor.putInt("finish", 0);
		editor.commit();
		
		Message fileBaiCu = (Message) ois.readObject();
		if (fileBaiCu instanceof BaiGiang) {
			System.out.println("OK...");
			
			Socket socket = new Socket(ipGV, 9999);
			System.out.println("Connecting...");
			
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();
			
		}
		
		ref = ctx.getSharedPreferences("GuiFileGV", 0);
		editor = ref.edit();
		editor.putInt("finish", 1);
		editor.commit();
	}
	
	public void guiTaiLieuChoBB(String path) throws IOException, ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];
		
		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		
		oos.writeObject(new BaiGiang(myfile.getName(), "" + myfile.length(),
				tenHS, mshs, ipHS));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof BaiGiang) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void guiFileBatKyChoGV(String path) throws IOException, ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];
		
		SharedPreferences prefs = ctx
				.getSharedPreferences("TaiKhoanGV", 0);
		String ipGV = prefs.getString("ip", "").toString();

		prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		
		oos.writeObject(new BatKy(myfile.getName(), ""+myfile.length(), tenHS, mshs, ipHS));
		
		SharedPreferences ref = ctx.getSharedPreferences("GuiFileGV", 0);
		SharedPreferences.Editor editor = ref.edit();
		editor.putInt("finish", 0);
		editor.commit();
		
		Message fileBaiCu = (Message) ois.readObject();
		if (fileBaiCu instanceof BatKy) {
			System.out.println("OK...");
			
			Socket socket = new Socket(ipGV, 9999);
			System.out.println("Connecting...");
			
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();
			
		}
		
		ref = ctx.getSharedPreferences("GuiFileGV", 0);
		editor = ref.edit();
		editor.putInt("finish", 1);
		editor.commit();
	}
	
	public void guiFileBatKyChoBB(String path) throws IOException, ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];
		
		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanHS", 0);
		String tenHS = prefs.getString("hoten", "").toString();
		String mshs = prefs.getString("mshs", "").toString();
		String ipHS = prefs.getString("ip", "").toString();
		
		oos.writeObject(new BatKy(myfile.getName(), "" + myfile.length(),
				tenHS, mshs, ipHS));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof BatKy) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void guiCapNhatKT15(String hoten, String mshs, String ip, String hoanthanh) throws IOException {
		oos.writeObject(new CapNhatKiemTra15(hoten, mshs, ip, hoanthanh));
	}

	public void nopBaiKT15(String _sender, String _mshs, String _host, String _hsDapAn)
			throws IOException, ClassNotFoundException {
		oos.writeObject(new KetQuaKiemTra15(_sender, _mshs, _host, _hsDapAn, ""));
	}
	
	public void guiCapNhatKT45() {
		//
	}

	public void nopBaiKT45() {
		//
	}

	public void run() {
		// TODO Auto-generated method stub

	}
}
